﻿@using Volo.Abp.UI.Navigation
@model ApplicationMenu

@if(Model.Items.Any())
{
    <ul class="nav justify-content-center">
        @foreach (var menuItem in Model.Items)
        {
            var elementId = string.IsNullOrEmpty(menuItem.ElementId) ? string.Empty : $"id=\"{menuItem.ElementId}\"";
            var cssClass = string.IsNullOrEmpty(menuItem.CssClass) ? string.Empty : menuItem.CssClass;
            var disabled = menuItem.IsDisabled ? "disabled" : string.Empty;
            var url = string.IsNullOrEmpty(menuItem.Url) ? "#" : Url.IsLocalUrl(menuItem.Url) ? Url.Content(menuItem.Url.EnsureStartsWith('~')) : menuItem.Url;
            var customComponentType = menuItem.GetComponentTypeOrDefault();
            if (menuItem.IsLeaf)
            {
                if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
                {
                    <li class="nav-item @cssClass" @elementId>
                        @(await Component.InvokeAsync(customComponentType))
                    </li>
                }
                else if (menuItem.Url != null)
                {
                    var useLocalePrefix = menuItem.IsSupportsLocalePrefix();
                    <li class="nav-item @cssClass" @elementId>
                        <a class="nav-link link-secondary @disabled" href="@url" add-culture="useLocalePrefix" target="@menuItem.Target">
                            @if (menuItem.Icon != null)
                            {
                                <i class="@menuItem.Icon"></i>
                            }
                            @menuItem.DisplayName
                        </a>
                    </li>
                }
            }
            else
            {
                @if (customComponentType != null && typeof(ViewComponent).IsAssignableFrom(customComponentType))
                {
                    <li class="nav-item">
                        @(await Component.InvokeAsync(customComponentType))
                    </li>
                }
                else
                {
                    <li class="nav-item dropdown">
                        <a class="nav-link link-secondary dropdown-toggle @disabled" href="#" id="Menu_@(menuItem.Name)" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                            @if (menuItem.Icon != null)
                            {
                                <i class="@menuItem.Icon"></i>
                            }
                            @menuItem.DisplayName
                        </a>
                        <ul class="dropdown-menu">
                            @foreach (var childMenuItem in menuItem.Items)
                            {
                                var childMenuItemUrl = string.IsNullOrEmpty(menuItem.Url) ? "#" : Url.IsLocalUrl(menuItem.Url) ? Url.Content(menuItem.Url.EnsureStartsWith('~')) : menuItem.Url;
                                var useLocalePrefix = menuItem.IsSupportsLocalePrefix();
                                <li>
                                    <a class="dropdown-item" href="@childMenuItemUrl" add-culture="useLocalePrefix">
                                        @if (menuItem.Icon != null)
                                        {
                                            <i class="@menuItem.Icon"></i>
                                        }
                                        @childMenuItem.DisplayName
                                    </a>
                                </li>
                            }
                        </ul>
                    </li>
                }
            }
        }
    </ul>
}